package cn.wolfcode.controller;


import cn.wolfcode.annotation.RequireLogin;
import cn.wolfcode.annotation.UserParam;
import cn.wolfcode.entity.Travel;
import cn.wolfcode.entity.Userinfo;
import cn.wolfcode.mongo.domain.TravelComment;
import cn.wolfcode.mongo.service.ITravelCommentMongoService;
import cn.wolfcode.query.TravelQuery;
import cn.wolfcode.service.ITravelContentService;
import cn.wolfcode.service.ITravelService;
import cn.wolfcode.service.IUserinfoService;
import cn.wolfcode.util.JSONResult;
import cn.wolfcode.util.UMEditorUploader;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author c1eye
 * @since 2022-11-03
 */
@RestController
@CrossOrigin
@RequestMapping("/travels")
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @GetMapping("/query")
    public JSONResult query(TravelQuery qo) {
        Page<Travel> list = travelService.queryPage(qo);
        return JSONResult.success(list);
    }


    @PostMapping("/contentImage")
    public String contentImage(MultipartFile upfile, HttpServletRequest request) throws Exception {
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif", ".png", ".jpg", ".jpeg", ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        /*
         * {
         *     name: 'xxx',
         *     originalName: 'xxxx',
         *     size: xxx,
         *     state: xxxx,
         *     type: 'xxxxx',
         *     url: 'xxxxx'
         * }
         * */
        String result = "{\"name\":\"" + up.getFileName() + "\", \"originalName\": \"" + up.getOriginalName() + "\", \"size\": " + up.getSize()
                + ", \"state\": \"" + up.getState() + "\", \"type\": \"" + up.getType() + "\", \"url\": \"" + up.getUrl() + "\"}";
        result = result.replaceAll("\\\\", "\\\\");
        if (callback == null) {
            return result;
        } else {
            return "<script>" + callback + "(" + result + ")</script>";
        }
    }

    @Autowired
    private ITravelContentService travelContentService;
    @Autowired
    private IUserinfoService userinfoService;

    @GetMapping("/detail")
    public JSONResult detail(Long id) {
        Travel travel = travelService.getById(id);
        travel.setContent(travelContentService.getById(id));
        travel.setAuthor(userinfoService.getById(travel.getAuthorId()));
        return JSONResult.success(travel);
    }


    @RequireLogin
    @PostMapping("/saveOrUpdate")
    public JSONResult saveOrUpdate(Travel travel, @UserParam Userinfo userinfo) {
        Long b = travelService.saveOrUpdate(travel, userinfo);
        return JSONResult.success(b);
    }
    @Autowired
    private ITravelCommentMongoService travelCommentMongoService;

    @GetMapping("/comments")
    public JSONResult comments(Long travelId) {
        return JSONResult.success(travelCommentMongoService.queryByTravelId(travelId));
    }

    @RequireLogin
    @PostMapping("/commentAdd")
    public JSONResult commentAdd(TravelComment comment, @UserParam Userinfo userinfo) {
        BeanUtils.copyProperties(userinfo, comment);
        comment.setUserId(userinfo.getId());
        comment.setId(null);
        travelCommentMongoService.save(comment);
        return JSONResult.success();
    }




}

